package com.wslds.blog.action.front;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.wslds.base.action.AbstractCRUDAction;
import com.wslds.base.util.BlogCons;
import com.wslds.base.util.Collects;
import com.wslds.base.util.Strings;
import com.wslds.bean.Review;
import com.wslds.blog.service.ReviewService;

@SuppressWarnings("serial")
@Component("reviewAction")
@Scope("prototype")
public class ReviewAction extends AbstractCRUDAction<Review, ReviewService> {

	private String url;
	
	public String listAjax(){
		instance.setExtraOrderColumns(" id desc ");
		return super.listAjax();
	}
	@SuppressWarnings("all")
	public String insert() {
		try{
			List<Long> ids = service.findExt("review_max_select", instance.getArtId());
			if( Collects.notNull(ids) ){
				instance.setSeq( ids.get(0) + 1);
			}else{
				instance.setSeq( 1L );
			}
			
			if( Strings.isNull(instance.getName())){
				instance.setName(this.getHttpRequest().getRemoteAddr());
			}else{
				instance.setName(instance.getName());
				getSession().put(BlogCons.SESSION.ACCESS_NAME, instance.getName());
			}
			
			if( Strings.notNull(instance.getWebSite())){
				String webSite = instance.getWebSite().replaceAll("http://", "");
				instance.setWebSite("http://" + webSite);
				
				getSession().put(BlogCons.SESSION.ACCESS_URL, webSite);
			}
			
			if( Strings.notNull(instance.getEmail())){
				instance.setEmail(instance.getEmail());
				getSession().put(BlogCons.SESSION.ACCESS_MAIL, instance.getEmail());
			}
			
			if( Strings.isNull(instance.getContent())){
				instance.setContent("此人没力，内容居然都不填");
			}
			
			/**
			 * 转换 < >
			 * 限制最多15行
			 */
			String pureData = instance.getContent();
			String[] splitData = pureData.split("\n");
			if( splitData.length > 15 ){
				StringBuffer sb = new StringBuffer();
				for( int i = 0; i < 15; i++ ){
					if( i < 14 ){
						sb.append(splitData[i] + "\n" );
					}
					else{
						sb.append(splitData[i]);
					}
				}
				pureData = sb.toString();
			}else if( pureData.trim().length() > 300 ) {
				pureData = pureData.trim().substring(0,300);
			}
			
			String simplePureData = "";
			if( pureData.length() > 50 ){
				simplePureData = pureData.substring(0,45) + "...";
			}else{
				simplePureData = pureData;
			}
			
			simplePureData = Strings.tranSmile(simplePureData);
			pureData = Strings.tranSmile(pureData);
			instance.setContent(pureData);
			instance.setSimpleContent(simplePureData);
			
			this.url = this.url.replaceAll("/wslds", "");
			instance.setCreateDate(new Date());
			service.save(instance);
			
			getResponse().getWriter().write("OK");
			
		}catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
		
		return NONE;
	}

	public String getUrl() {
		return url;
	}


	public void setUrl(String url) {
		this.url = url;
	}
	@Override
	@Resource(type=ReviewService.class)
	public void setService(ReviewService service) {
		this.service = service;
	}

}
